package com.share.module.business.echart.service;

import com.share.module.business.echart.vo.HomeReqVO;
import com.share.module.business.echart.vo.HomeVO;
import com.share.module.business.stock.entity.Stock;
import com.share.module.business.stock.service.StockService;
import com.share.module.business.stockprofit.entity.StockProfit;
import com.share.module.business.stockprofit.service.StockProfitService;
import com.share.module.business.stockrevenuequarter.entity.StockRevenueQuarter;
import com.share.module.business.stockrevenuequarter.service.StockRevenueQuarterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class EchartsService {

    @Resource
    private StockService stockService;
    @Resource
    private StockProfitService stockProfitService;
    @Resource
    private StockRevenueQuarterService stockRevenueQuarterService;

    public HomeVO home(HomeReqVO vo) {
        // 搜索条件为空时，默认取第一个
        Stock stock = stockService.getFirst(vo.getStockCode(), vo.getStockName());

        List<StockRevenueQuarter> stockRevenueQuarters = stockRevenueQuarterService.findByStockCode(stock.getCode());

        List<LocalDate> x = new ArrayList<>(stockRevenueQuarters.size() << 2);
        List<BigDecimal> y = new ArrayList<>(stockRevenueQuarters.size() << 2);
        List<BigDecimal> quarterGrowth = new ArrayList<>(stockRevenueQuarters.size() << 2);
        stockRevenueQuarters.forEach(item -> {
            x.add(item.getQ1End());
            x.add(item.getQ2End());
            x.add(item.getQ3End());
            x.add(item.getQ4End());

            y.add(item.getQ1Revenue());
            y.add(item.getQ2Revenue());
            y.add(item.getQ3Revenue());
            y.add(item.getQ4Revenue());
        });

        // 根据营业收入计算环比增长
        for (int i = 0; i < y.size(); i++) {
            if (i == 0) {
                quarterGrowth.add(BigDecimal.ZERO);
            } else {
                BigDecimal subtract = y.get(i).subtract(y.get(i - 1));
                BigDecimal divide = subtract.divide(y.get(i - 1).abs(), 4, RoundingMode.HALF_UP);
                // 如果最后一个值是0，则将值设为0
                if (i == y.size() - 1 && y.get(i).compareTo(BigDecimal.ZERO) == 0) {
                    quarterGrowth.add(BigDecimal.ZERO);
                } else {
                    quarterGrowth.add(divide.multiply(new BigDecimal("100")));
                }
            }
        }

        // 根据营业收入计算同比增长
        List<BigDecimal> yearGrowth = new ArrayList<>(stockRevenueQuarters.size() << 2);
        for (int i = 0; i < y.size(); i++) {
            if (i == 0 || i == 1 || i == 2 || i == 3) {
                yearGrowth.add(BigDecimal.ZERO);
            } else {
                BigDecimal subtract = y.get(i).subtract(y.get(i - 4));
                BigDecimal divide = subtract.divide(y.get(i - 4).abs(), 4, RoundingMode.HALF_UP);
                // 如果最后一个值是0，则将值设为0
                if (i == y.size() - 1 && y.get(i).compareTo(BigDecimal.ZERO) == 0) {
                    yearGrowth.add(BigDecimal.ZERO);
                } else {
                    yearGrowth.add(divide.multiply(new BigDecimal("100")));
                }
            }
        }

        // 查询净利润表
        List<StockProfit> stockProfits = stockProfitService.findByStockCode(stock.getCode());

        List<BigDecimal> profit = new ArrayList<>(stockProfits.size() << 2);
        stockProfits.forEach(item -> {
            profit.add(item.getQ1NetProfit());
            profit.add(item.getQ2NetProfit());
            profit.add(item.getQ3NetProfit());
            profit.add(item.getQ4NetProfit());
        });

        // 净利润环比
        List<BigDecimal> profitChain = new ArrayList<>(stockProfits.size() << 2);
        for (int i = 0; i < profit.size(); i++) {
            if (i == 0) {
                profitChain.add(BigDecimal.ZERO);
                continue;
            }
            BigDecimal subtract = profit.get(i).subtract(profit.get(i - 1));
            BigDecimal divide = subtract.divide(profit.get(i - 1).abs(), 4, RoundingMode.HALF_UP);
            // 如果最后一个值是0，则将值设为0
            if (i == profit.size() - 1 && profit.get(i).compareTo(BigDecimal.ZERO) == 0) {
                profitChain.add(BigDecimal.ZERO);
            } else {
                profitChain.add(divide.multiply(new BigDecimal("100")));
            }
        }

        // 净利润同比增长
        List<BigDecimal> profitYear = new ArrayList<>(stockProfits.size() << 2);
        for (int i = 0; i < profit.size(); i++) {
            if (i == 0 || i == 1 || i == 2 || i == 3) {
                profitYear.add(BigDecimal.ZERO);
            } else {
                BigDecimal subtract = profit.get(i).subtract(profit.get(i - 4));
                BigDecimal divide = subtract.divide(profit.get(i - 4).abs(), 4, RoundingMode.HALF_UP);
                if (i == profit.size() - 1 && profit.get(i).compareTo(BigDecimal.ZERO) == 0) {
                    profitYear.add(BigDecimal.ZERO);
                } else {
                    profitYear.add(divide.multiply(new BigDecimal("100")));
                }
            }
        }

        // 查询扣非净利润
        List<BigDecimal> noneProfit = new ArrayList<>(stockProfits.size() << 2);
        stockProfits.forEach(item -> {
            noneProfit.add(item.getQ1NoneNetProfit());
            noneProfit.add(item.getQ2NoneNetProfit());
            noneProfit.add(item.getQ3NoneNetProfit());
            noneProfit.add(item.getQ4NoneNetProfit());
        });

        // 扣非净利润(单季度)环比
        List<BigDecimal> noneProfitChain = new ArrayList<>(stockProfits.size() << 2);
        for (int i = 0; i < noneProfit.size(); i++) {
            if (i == 0) {
                noneProfitChain.add(BigDecimal.ZERO);
                continue;
            }
            BigDecimal subtract = noneProfit.get(i).subtract(noneProfit.get(i - 1));
            BigDecimal divide = subtract.divide(noneProfit.get(i - 1).abs(), 4, RoundingMode.HALF_UP);

            // 如果最后一个值是0，则将值设为0
            if (i == noneProfit.size() - 1 && noneProfit.get(i).compareTo(BigDecimal.ZERO) == 0) {
                noneProfitChain.add(BigDecimal.ZERO);
            } else {
                noneProfitChain.add(divide.multiply(new BigDecimal("100")));
            }
        }

        // 扣非净利润(单季度)同比增长
        List<BigDecimal> noneProfitYear = new ArrayList<>(stockProfits.size() << 2);
        for (int i = 0; i < noneProfit.size(); i++) {
            if (i == 0 || i == 1 || i == 2 || i == 3) {
                noneProfitYear.add(BigDecimal.ZERO);
            } else {
                BigDecimal subtract = noneProfit.get(i).subtract(noneProfit.get(i - 4));
                BigDecimal divide = subtract.divide(noneProfit.get(i - 4).abs(), 4, RoundingMode.HALF_UP);
                if (i == noneProfit.size() - 1 && noneProfit.get(i).compareTo(BigDecimal.ZERO) == 0) {
                    noneProfitYear.add(BigDecimal.ZERO);
                } else {
                    noneProfitYear.add(divide.multiply(new BigDecimal("100")));
                }
            }
        }
        HomeVO homeVO = new HomeVO();
        homeVO.setStockCode(stock.getCode());
        homeVO.setStockName(stock.getName());
        homeVO.setX(x);
        homeVO.setY(y);
        homeVO.setQuarterGrowth(quarterGrowth);
        homeVO.setYearGrowth(yearGrowth);
        homeVO.setProfit(profit);
        homeVO.setProfitChain(profitChain);
        homeVO.setProfitYear(profitYear);
        homeVO.setNoneProfit(noneProfit);
        homeVO.setNoneProfitChain(noneProfitChain);
        homeVO.setNoneProfitYear(noneProfitYear);
        return homeVO;
    }

    public static void main(String[] args) {
        BigDecimal subtract = BigDecimal.valueOf(2).subtract(BigDecimal.valueOf(-1));
        System.out.println(subtract);
    }
}
